//
//  SEInquirySettingViewController.swift
//  SmartEyeForDoctor
//
//  Created by qdsg on 2021/5/5.
//

import UIKit

class SEInquirySettingViewController: SEBaseViewController {

    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.tableFooterView = tableFooterView
            tableView.rowHeight = UITableView.automaticDimension
            tableView.keyboardDismissMode = .onDrag
            tableView.register(SEInquirySettingFeeCell.nibForCell(), forCellReuseIdentifier: SEInquirySettingFeeCell.reuseIdentifier())
            tableView.register(SEInquirySettingDescribeCell.nibForCell(), forCellReuseIdentifier: SEInquirySettingDescribeCell.reuseIdentifier())
        }
    }

    private lazy var tableFooterView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: 50 + 48 + 30))
        let btnView = SEConfirmBtnView(frame: CGRect(x: 15, y: 50, width: UIScreen.screenWidth - 30, height: 48))
        btnView.center = view.center
        btnView.delegate = self
        btnView.title = "确认保存"
        view.addSubview(btnView)
        return view
    }()

    var viewModel = SEInquirySettingViewModel(withLoading: true)

    init(with userInfoModel: SEUserInfoModel) {
        viewModel.userInfoModel = userInfoModel
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "问诊设置"
        addEndEditingTap()
        // Do any additional setup after loading the view.
    }

    override func setupUI() {
        super.setupUI()

        viewModel.updateData = { status in
            switch status {
            case .success:
                self.navigationController?.popViewController(animated: true)
            case .auth(let msg):
                self.presentLoginViewController(selector: #selector(self.setSingleInquiryPrice)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

}

// MARK: - UITableViewDataSource, UITableViewDelegate

extension SEInquirySettingViewController: UITableViewDataSource, UITableViewDelegate {

    // MARK: - UITableViewDataSource

    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: SEInquirySettingFeeCell.reuseIdentifier(), for: indexPath) as! SEInquirySettingFeeCell
            cell.delegate = self
            cell.updateData(viewModel.settingFeeCellModel, indexPath: indexPath)
            return cell
        }
        if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: SEInquirySettingDescribeCell.reuseIdentifier(), for: indexPath)
            return cell
        }
        return UITableViewCell()
    }

    // MARK: - UITableViewDelegate

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.clear
        return view
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        switch section {
        case 0: return 15
        case 1: return 10
        default: return 0
        }
    }
}

// MARK: - SEConfirmBtnViewDelegate

extension SEInquirySettingViewController: SEConfirmBtnViewDelegate {
    func confirmBtnViewAction(view: SEConfirmBtnView) {
        self.view.endEditing(true)
        setSingleInquiryPrice()
    }
}

// MARK: - SEInquirySettingFeeCellDelegate

extension SEInquirySettingViewController: SEInquirySettingFeeCellDelegate {
    func inquirySettingFeeCelldidEndEdit(cell: SEInquirySettingFeeCell, indexPath: IndexPath) {
        viewModel.userInfoModel.fee = (Double(cell.textField.text!) ?? 0) * 100.0
        tableView.reloadData()
    }
}

// MARK: - APIRequest
extension SEInquirySettingViewController {
    @objc private func setSingleInquiryPrice() {
        viewModel.setSingleInquiryPrice()
    }
}

